﻿Public Class TypeAdd

    Private _typeid As New List(Of String)
    Public Property 软件分类类型() As List(Of String)
        Get
            _typeid.Clear()
            For Each gr As DataGridViewRow In Me.dgType.Rows
                If CBool(gr.Cells(0).Value) Then
                    _typeid.Add(gr.Cells("colNao").Value)
                End If
            Next
            Return _typeid
        End Get
        Set(ByVal value As List(Of String))
            For Each gr As DataGridViewRow In Me.dgType.Rows
                For Each Str As String In value
                    If CStr(gr.Cells("colNao").Value) = Str Then
                        gr.Cells(0).Value = True
                    End If
                Next
            Next
            _typeid = value
        End Set
    End Property

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        of1.InitialDirectory = Util.GetCurrentFolderIcon
        of1.Filter = "ico|*.ico|bmp|*.bmp|png|*.png|gif|*.gif|all|*.*"
        of1.FilterIndex = 0

        of1.FileName = pb.Tag.ToString
        If of1.ShowDialog = Windows.Forms.DialogResult.OK Then
            pb.Image = Image.FromFile(of1.FileName).GetThumbnailImage(32, 32, Nothing, IntPtr.Zero)
            pb.Tag = of1.FileName
            If of1.FileName.ToLower.Contains(Util.GetCurrentFolderIcon.ToLower) Then
                Me.chk相对路径.Checked = True
            Else
                Me.chk相对路径.Checked = False
            End If
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If checkDb() = False Then
            Return
        End If

        软件类型.Id = txtId.Text
        软件类型.Name = txtName.Text.Trim
        软件类型.SoftWareBigType = 软件分类类型
        软件类型.IconPath = pb.Tag.ToString
        If chk相对路径.Checked Then
            软件类型.IconpathType = "0"
            软件类型.IconPath = System.IO.Path.GetFileName(pb.Tag.ToString)
        Else
            软件类型.IconpathType = "1"
        End If

        Dim tyU As New XMLSoftWareType
        If Me.Text = "添加软件小分类" Then
            tyU.Create(软件类型)
        Else
            tyU.Update(软件类型)
        End If
        Dim fendir As String = System.IO.Path.Combine(Util.GetCurrentFolderSoftWare, 软件类型.Id)
        If Not System.IO.Directory.Exists(fendir) Then
            System.IO.Directory.CreateDirectory(fendir)
        End If
        If Me.Text = "添加软件小分类" Then
            RaiseEvent Updateparent()
            Me.AddInit()
            Return
        Else
            wys.util.Common.MessageBase.ShowMessage("更新成功")
        End If

        DialogResult = Windows.Forms.DialogResult.OK

    End Sub

    Public Event Updateparent()
    Private Sub AddInit()
        Dim tey As New XMLSoftWareType
        软件分类类型 = _软件类型.SoftWareBigType
        Me.txtId.Text = tey.GetMaxid

        Me.txtName.Text = ""
        Me.pb.Tag = ""
        Me.pb.Image = Nothing
        Me.chk相对路径.Checked = False

    End Sub

    Private Sub UpdateInit()
        Dim tey As New XMLSoftWareType
        _软件类型 = tey.GetById(_软件类型.Id)
        软件分类类型 = _软件类型.SoftWareBigType
        Me.txtName.Text = _软件类型.Name
        Me.pb.Tag = ""
        Dim filePath As String = Util.GetCurrentFileIcon(_软件类型.IconPath, CInt(_软件类型.IconpathType))
        If System.IO.File.Exists(filePath) Then
            Me.pb.Image = Image.FromFile(filePath).GetThumbnailImage(32, 32, Nothing, IntPtr.Zero)
            Me.pb.Tag = Util.GetCurrentFileIcon(_软件类型.IconPath, CInt(_软件类型.IconpathType))
        End If
        If _软件类型.IconpathType = "0" Then
            Me.chk相对路径.Checked = True
        Else
            Me.chk相对路径.Checked = False
        End If

    End Sub

    Private _软件类型 As CSoftWareType = New CSoftWareType
    Public Property 软件类型() As CSoftWareType
        Get
            Return _软件类型
        End Get
        Set(ByVal value As CSoftWareType)
            _软件类型 = value
        End Set
    End Property

    Private Sub RefreshForm()
        Dim totalWidth As Integer = dgType.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)
        dgType.Width = totalWidth
        If dgType.DisplayedRowCount(False) <> dgType.RowCount Then
            totalWidth = totalWidth + SystemInformation.BorderSize.Width + SystemInformation.VerticalScrollBarWidth + dgType.Margin.Right * 2
            dgType.Width = dgType.Width + SystemInformation.VerticalScrollBarWidth + dgType.Margin.Right
        Else
            totalWidth = totalWidth + SystemInformation.BorderSize.Width * 2 + dgType.Margin.Left + dgType.Margin.Right
        End If
        Me.Width = totalWidth + 3 + dgType.Left
    End Sub

    Private Sub TypeAdd_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try

            Dim dt As DataTable = Util.GetSoftWareBigType

            Me.dgType.DataSource = dt

            For index As Integer = 1 To Me.dgType.ColumnCount - 1
                Me.dgType.Columns(index).ReadOnly = False
            Next

            Me.txtId.Text = _软件类型.Id
            If Me.Text = "添加分类" Then
                AddInit()
            Else
               UpdateInit
            End If

            RefreshForm()
        Catch ex As Exception

        End Try

    End Sub

    Private Function checkDb() As Boolean

        If Me.txtName.Text.Trim = "" Then
            wys.util.Common.MessageBase.ShowMessage("名称不能不空")
            txtName.Focus()
            Return False
        End If

        'If 软件分类类型.Count = 0 Then
        '    wys.util.Common.MessageBase.ShowMessage("必须选择一个软件类型")
        '    Me.dgType.Focus()
        '    Return False
        'End If

        If Not System.IO.File.Exists(pb.Tag.ToString) Then
            wys.util.Common.MessageBase.ShowMessage("图片路径不能为空")
            Me.btnSelect.Focus()
            Return False
        End If

        Return True

    End Function

    Public Sub New(ByVal 软件类型 As CSoftWareType)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        _软件类型 = 软件类型
    End Sub
End Class
